<?php
class Withdraw_model extends BZ_Model
{
    protected $_table  = 'withdraw';
    protected $_table1 = 'withdraw_audit';
    
    /**
     * 根据id查找
     */
    public function findById($id, $uid)
    {
        $this->db->select('withdraw_id, uid, user_name, amount, amount_carry, bank_id, bank_card, cardholder, pin, mobile_phone, bank_name, status, state,created_at, pay_time, updated_at');
        $this->db->from($this->_table);
        $this->db->where('withdraw_id', (int)$id);
        $this->db->where('uid', (int)$uid);
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 查找每个用户的提现记录
     */
    public function page_list($params, $start, $count)
    {
        $this->db->select('withdraw.withdraw_id, amount, withdraw.state, withdraw.created_at, withdraw_audit.state AS audit_state, withdraw_audit.describe , LEFT(`withdraw`.`created_at`, 7) AS month', FALSE);
        $this->db->from($this->_table);
        $this->db->join("(SELECT withdraw_audit.withdraw_id, withdraw_audit.describe, withdraw_audit.state FROM withdraw_audit GROUP BY withdraw_audit.withdraw_id ORDER BY `withdraw_audit`.`audit_id` DESC) AS withdraw_audit", 
            'withdraw.withdraw_id=withdraw_audit.withdraw_id', 'LEFT');
        $this->db->where('uid', $params['uid']);
        $this->db->where('status', 2); //已付
        if ( ! empty($params['withdraw_id'])) {
            $this->db->where('withdraw.withdraw_id', $params['withdraw_id']);
        }
        if ( ! empty($params['state'])) {
            $this->db->where_in('withdraw.state', $params['state']);//审核阶段
        }
        if ( ! empty($params['audit_state'])) {
            $this->db->where('withdraw_audit.state', $params['audit_state']);//审核状态
        }
        if (!empty($params['startData'])) {
            $this->db->where('`withdraw`.`created_at` >=', $params['startData']. ' 00:00:00');
        }
        if (!empty($params['endData'])) {
            $this->db->where('`withdraw`.`created_at` <=', $params['endData'].' 23:59:59');
        }
        $this->db->order_by('withdraw.withdraw_id', 'DESC');
        $this->db->limit($start, $count);
        return $this->db->get();
    }

    /**
     * 统计总数
     */
    public function total($params)
    {
        $this->db->select('withdraw_id');
        $this->db->from($this->_table);
        $this->db->join("(SELECT withdraw_audit.withdraw_id, withdraw_audit.describe, withdraw_audit.state FROM withdraw_audit GROUP BY withdraw_audit.withdraw_id ORDER BY `withdraw_audit`.`audit_id` DESC) AS withdraw_audit", 
            'withdraw.withdraw_id=withdraw_audit.withdraw_id', 'LEFT');
        $this->db->where('uid', $params['uid']);
        $this->db->where('status', 2);
        if ( ! empty($params['withdraw_id'])) {
            $this->db->where('withdraw.withdraw_id', $params['withdraw_id']);
        }
        if ( ! empty($params['state'])) {
            $this->db->where_in('withdraw.state', $params['state']);//审核阶段
        }
        if ( ! empty($params['audit_state'])) {
            $this->db->where('withdraw_audit.state', $params['audit_state']);//审核状态
        }
        if (!empty($params['startData'])) {
            $this->db->where('`withdraw`.`created_at` >=', $params['startData']. ' 00:00:00');
        }
        if (!empty($params['endData'])) {
            $this->db->where('`withdraw`.`created_at` <=', $params['endData'].' 23:59:59');
        }
        return $this->db->count_all_results();
    }

    /**
     * 添加
     */
    public function insertWithdraw($postData, $bank)
    {
        $data = array(
            'uid'            => $postData['uid'],
            'user_name'    => $postData['user_name'],
            'amount'        => $postData['amount'],
            'amount_carry' => $postData['amount_carry'],
            'bank_id'        => $bank->bank_id,
            'bank_name'    => $bank->bank_name,
            'bank_card'    => $bank->bank_card,
            'cardholder'   => $bank->cardholder,
            'pin'            => $bank->pin,
            'mobile_phone' => $bank->mobile_phone,
            'is_from'        => 1,
            'status'        => 1,
            'state'        => 0,
            'created_at'   => date('Y-m-d H:i:s'),
            'pay_time'       => '0000-00-00 00:00:00',
            'updated_at'   => '0000-00-00 00:00:00',
        );
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }

    /**
     * 查找每个用户的提现记录
     */
    public function withdrawList($params, $count, $start)
    {
        $this->db->select('amount, amount_carry, state, created_at, updated_at');
        $this->db->from($this->_table);
        $this->db->where('uid', $params['uid']);
        $this->db->order_by('withdraw_id', 'DESC');
        $this->db->limit($start, $count);
        return $this->db->get();
    }

    /**
     * 统计总数
     */
    public function withdrawTotal($params)
    {
        $this->db->select('withdraw_id');
        $this->db->from($this->_table);
        $this->db->where('uid', $params['uid']);
        return $this->db->count_all_results();
    }

    /**
     * 更新支付状态
     */
    public function updatePaystatus($id, $uid)
    {
        $data = array(
            'status' => 2, //支付
            'pay_time' => date('Y-m-d H:i:s')
        );
        $this->db->where('withdraw_id', (int)$id);
        $this->db->where('uid', (int)$uid);
        return $this->db->update($this->_table, $data);
    }
}